package com.nice.app.controller;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.nice.app.domain.User;

/**
 * Handles requests for the application home page.
 */
@Controller
public class HomeController {
	
	@Autowired
    Comparator<String> comparator;
	
	@RequestMapping(value = "/home")
	public String home() {
        System.out.println("HomeController: Passing through... : home page");
        return "home";
    }
	
	@RequestMapping(value = "/compare", method = RequestMethod.GET)
    public String compare(@RequestParam("input1") String input1,
            @RequestParam("input2") String input2, Model model) {
 
        int result = comparator.compare(input1, input2);
        String inEnglish = (result < 0) ? "less than" : (result > 0 ? "greater than" : "equal to");
 
        String output = "According to our Comparator, '" + input1 + "' is " + inEnglish + "'" + input2 + "'";
 
        model.addAttribute("output", output);
        
        List<User> users = new ArrayList();
        User u1 = new User();
        u1.setFirstName("f1");
        User u2 = new User();
        u2.setFirstName("f2");
        
        users.add(u1);
        users.add(u2);
        
        model.addAttribute("userList", users);
        
        return "compareResult";
    }
}
